package io.github.zhibaibubai.blog.util;

import io.github.zhibaibubai.blog.MyDBUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class WebsiteUtil {
    public static void main(String[] args) {
        String sql = "select id, url from website order by id asc";
        Connection connection = MyDBUtil.getConnection();
        try (
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                ResultSet rs = preparedStatement.executeQuery();
        ) {
            while (rs.next()) {
                int id = rs.getInt("id");
                String url = rs.getString("url");

                String ogTitle = "";
                String ogDescription = "";
                String ogImage = "";

                System.out.println("正在访问 url = " + url);
                Document document = Jsoup.connect(url).get();

                // og:title
                Element ogTitleElement = document.selectFirst("meta[property=og:title]");
                if (ogTitleElement == null) {
                    System.out.println("该网站没有配置 og:title，接着解析 og:description");
                } else {
                    ogTitle = ogTitleElement.attr("content");
                    System.out.println("已解析出该网站的 og:title = " + ogTitle);
                }

                // og:description
                Element ogDescriptionElement = document.selectFirst("meta[property=og:description]");
                if (ogDescriptionElement == null) {
                    System.out.println("该网站没有配置 og:description，接着解析 og:image");
                } else {
                    ogDescription = ogDescriptionElement.attr("content");
                    System.out.println("已解析出该网站的 og:description = " + ogDescription);
                }

                // og:image
                Element ogImageElement = document.selectFirst("meta[property=og:image]");
                if (ogImageElement == null) {
                    System.err.println("该网站没有配置 og:image，停止解析当前网站 url=" + url);
                    continue;
                } else {
                    ogImage = ogImageElement.attr("content");
                    System.out.println("已解析出该网站的 og:image = " + ogImage);
                }

                System.out.println("正在更新该网站的 og 信息 url = " + url);
                String updateSql = "update website set og_title=?, og_description=?, og_image=? where id=?";
                try (PreparedStatement preparedStatement1 = connection.prepareStatement(updateSql)) {
                    preparedStatement1.setString(1, ogTitle);
                    preparedStatement1.setString(2, ogDescription);
                    preparedStatement1.setString(3, ogImage);
                    preparedStatement1.setInt(4, id);
                    preparedStatement1.executeUpdate();
                    System.out.println("更新完成！url = " + url);
                }
            }
        } catch (SQLException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}